<?php
/**
 * Create by 11li
 * Date 2020/1/9
 * Time 21:25
 * 收货地址
 */

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\service\AddressService;
use app\common\service\StatusService;

class Address extends Api {
	
	/**
	 * 列表
	 */
	public function index(){
		$model = new \app\common\model\Address();
		$res = $model->getUserAddress($this->auth->id);
		$this->success('查询成功', $res);
	}
	
	/**
	 * 添加编辑收货地址
	 */
	public function edit(){
		$data = $this->request->post();
		$check = new \app\common\validate\Address();
		if(!$check->check($data)) {
			$this->error($check->getError());
		}
		
		$obj = new AddressService();
		$res = $obj->saveAddress($this->auth->id, $data);
		if($res !== false) {
			$this->success(!empty($data['id']) ? '保存成功' :'添加成功');
		} else {
			$this->error('添加出错');
		}
	}
	
	/**
	 * 删除收货地址
	 */
	public function del($id){
		$model = new \app\common\model\Address();
		$res = $model->save(
			['status' => StatusService::STATUS_DELETE],
			['id' => $id, 'user_id' => $this->auth->id]
		);
		if($res !== false) {
			$this->success('删除成功');
		} else {
			$this->error('删除出错');
		}
	}
}
